package com.hooya.fa.eu.biz.business.service;


import com.hooya.fa.eu.biz.dao.entity.InvInvoice;
import com.hooya.fa.eu.biz.dao.entity.InvInvoiceDtl;
import com.hooya.fa.eu.biz.dao.entity.dto.InvoiceDataDTO;
import com.hooya.fa.eu.biz.dao.mapper.InvInvoiceDtlMapper;
import com.hooya.fa.eu.biz.dao.mapper.InvInvoiceMapper;
import com.hooya.fa.eu.biz.dao.mapper.MetaCorpHeaderMapper;
import com.hooya.fa.eu.biz.dao.mapper.StkOrigDataCollectMapper;
import com.hooya.fa.eu.biz.service.impl.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class Insert_Transfer_Invoice {

    @Autowired
    private StkOrigDataCollectMapper stkOrigdataCollectMapper;

    @Autowired
    private InvInvoiceMapper invInvoiceMapper;

    @Autowired
    private InvInvoiceDtlMapper invInvoiceDtlMapper;

    @Autowired
    private MetaCorpHeaderMapper metaCorpHeaderMapper;

    @Autowired
    private CommonService commonService;

    @Autowired
    private Insert_Add_Invoice insert_add_invoice;

    /**
     * 插入转运发票方法
     */
    @Transactional
    public void insert_transfer_invoice(String start) {
        try {
            log.info("开始执行插入转运发票步骤，开始日期：{}", start);

            // 处理日期
            Map<String, String> dateMap = commonService.dateChange(start);
            String nextMonthStart = dateMap.get("nextMonthStart");

            // 获取原始转运数据
            Map<String, Object> origDataMap = getOrigTranData(start, nextMonthStart);
            List<InvoiceDataDTO> origGenerateData = (List<InvoiceDataDTO>) origDataMap.get("generateData");
            List<InvoiceDataDTO> origDetailData = (List<InvoiceDataDTO>) origDataMap.get("detailData");


            // 获取税号数据 -- 复用add
            List<InvoiceDataDTO> taxIdData = insert_add_invoice.getTaxid();
            // 生成发票明细 -- 复用add
            List<InvoiceDataDTO> invoiceDtl = insert_add_invoice.generateInvoiceDtl(origDetailData);
            // 添加信息 -- 复用add
            List<InvoiceDataDTO> detailData = insert_add_invoice.addInfos(invoiceDtl);
            // 添加税号 -- 复用add
            List<InvoiceDataDTO> generateData = insert_add_invoice.addTaxid(origGenerateData,taxIdData);


            // 转换为实体类并保存
            List<InvInvoice> invInvoices = insert_add_invoice.convertToInvInvoice(generateData);
            List<InvInvoiceDtl> invInvoiceDtls = insert_add_invoice.convertToInvInvoiceDtl(detailData);
            // 批量插入数据
            insert_add_invoice.batchInsertInvoices(invInvoices);
            insert_add_invoice.batchInsertInvoiceDtls(invInvoiceDtls);

            log.info("插入转运发票步骤执行完成");
        } catch (Exception e) {
            log.error("插入转运发票步骤执行失败", e);
            throw new RuntimeException("插入转运发票步骤执行失败", e);
        }
    }

    /**
     * 获取原始转运数据
     */
    private Map<String, Object> getOrigTranData(String start, String nextMonthStart) {
        Map<String, Object> result = new HashMap<>();

        // 查询所有库存数据
        List<InvoiceDataDTO> tranGenerateData = stkOrigdataCollectMapper.getTranGenerateData(start, nextMonthStart);
        tranGenerateData.forEach(f -> {
            f.setFullseller(f.getFullseller().toLowerCase());
            f.setFullbuyer(f.getFullbuyer().toLowerCase());
        });

        List<InvoiceDataDTO> tranDetailData = stkOrigdataCollectMapper.getTranDetailData(start, nextMonthStart);

        result.put("generateData", tranGenerateData);
        result.put("detailData", tranDetailData);

        return result;
    }

}